package org.sn.jdish.spring.demo.biz;

import java.io.UnsupportedEncodingException;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class RedisBiz {

	@Autowired
	private RedisTemplate<String, Object> redisTemplate;

	private static String redisCode = "utf-8";

	public String ping() {
		return (String) redisTemplate.execute(new RedisCallback<Object>() {
			public String doInRedis(RedisConnection connection) throws DataAccessException {
				return connection.ping();
			}
		});
	}

	public long dbSize() {
		return (Long) redisTemplate.execute(new RedisCallback<Object>() {
			public Long doInRedis(RedisConnection connection) throws DataAccessException {
				return connection.dbSize();
			}
		});
	}

	public String flushDB() {
		return (String) redisTemplate.execute(new RedisCallback<Object>() {
			public String doInRedis(RedisConnection connection) throws DataAccessException {
				connection.flushDb();
				return "ok";
			}
		});
	}

	public boolean exists(final String key) {
		return (Boolean) redisTemplate.execute(new RedisCallback<Object>() {
			public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
				return connection.exists(key.getBytes());
			}
		});
	}

	public Set<String> Setkeys(String pattern) {
		return redisTemplate.keys(pattern);
	}

	public String get(final String key) {
		return (String) redisTemplate.execute(new RedisCallback<Object>() {
			public String doInRedis(RedisConnection connection) throws DataAccessException {
				try {
					return new String(connection.get(key.getBytes()), redisCode);
				} catch (UnsupportedEncodingException e) {
					e.printStackTrace();
				}
				return "";
			}
		});
	}

	public void set(String key, String value) {
		this.set(key, value, 0L);
	}

	public void set(byte[] key, byte[] value) {
		this.set(key, value, 0L);
	}

	public void set(String key, String value, long seconds) {
		this.set(key.getBytes(), value.getBytes(), seconds);
	}

	public void set(final byte[] key, final byte[] value, final long seconds) {
		redisTemplate.execute(new RedisCallback<Object>() {
			public Object doInRedis(RedisConnection connection) throws DataAccessException {
				connection.set(key, value);
				if (seconds > 0) {
					connection.expire(key, seconds);
				}
				return 1L;
			}
		});
	}

	public Long del(final String... keys) {
		return (Long) redisTemplate.execute(new RedisCallback<Object>() {
			public Long doInRedis(RedisConnection connection) throws DataAccessException {
				for (String key : keys) {
					return connection.del(key.getBytes());
				}
				return null;
			}
		});
	}

}
